package com.games.hanged.backingbean;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

@ManagedBean(name="imageBackingBean")
@SessionScoped
public class ImageBackingBean implements Serializable {
	private static final long serialVersionUID = 3685666854673037497L;
	private BufferedImage bufferedImage = null;
	
	public void addError(int errors){
		Graphics graphics = bufferedImage.getGraphics();
		if(errors == 1){
            graphics.setColor(Color.red);
            graphics.drawOval(163,50,40,40);
        }
        if(errors == 2){
            graphics.setColor(Color.red);
            graphics.drawLine(182,90,182,140);
        }
        if(errors == 3){
            graphics.setColor(Color.red);
            graphics.drawLine(181,100,155,120);
        }
        if(errors == 4){
            graphics.setColor(Color.red);
            graphics.drawLine(183,100,215,120);
        }
        if(errors == 5){
            graphics.setColor(Color.red);
            graphics.drawLine(182,140,155,170);
        }
        if(errors == 6){
            graphics.setColor(Color.red);
            graphics.drawLine(183,140,210,170);
        }
	}

	public void action(){
		bufferedImage = new BufferedImage(300, 230, BufferedImage.TYPE_INT_RGB);
		Graphics graphics = bufferedImage.getGraphics();
		graphics.setColor(Color.white);
		graphics.fillRect(2,2,296,226);
		graphics.setColor(Color.black);
		graphics.fillRect(60,30,20,190);
		graphics.setColor(Color.black);
		graphics.fillRect(60,10,190,20);
		graphics.setColor(Color.gray);
		graphics.fillRect(183,30,5,20);
	}
	
	public void showImage(HttpServletResponse response) {
		try {
			response.setContentType("image/png");
			ImageIO.write(bufferedImage, "png", response.getOutputStream());
		} catch (IOException e) {
			System.out.println("Showing image failed, I/O error");
		}
	}

	public BufferedImage getBufferedImage() {
		return bufferedImage;
	}

	public void setBufferedImage(BufferedImage bufferedImage) {
		this.bufferedImage = bufferedImage;
	}
	
}